<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu.tld" prefix="menu" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu-el.tld" prefix="menu-el" %>
<%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tlds/fmt.tld" prefix="fmt" %>
<%@ taglib uri="/WEB-INF/tlds/authz.tld" prefix="authz" %>
<%@ taglib uri="/WEB-INF/tlds/sitemesh-decorator.tld" prefix="decorator"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" scope="request"/>
<script type="text/javascript" src="${ctx}/dwr/interface/ConfigSaverDWR.js"></script>
<script type="text/javascript" src="${ctx}/dwr/interface/ValidationHelperDWR.js"></script>
<script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx}/dwr/util.js"></script>
<script type="text/javascript">
<!--
	function saveConnection() {
		var url = $F("url");
		var login = $F("user");
		var pwd = $F("pass");
		login = trim(login);
		pwd = trim(pwd);
		spec = unAllowedChars(login,'<bean:message key="allowedLoginCharacters"/>');
		if(!url || url.length==0 || url.indexOf('ldap:\/\/')!=0) {
			alert('<bean:message key="error.urlNotValid"/>');
		} else if(!login || login.length==0) {
			alert('<bean:message key="error.loginFieldEmpty"/>'); 
		} else if(!pwd || pwd.length==0) {
			alert('<bean:message key="error.passwordFieldEmpty"/>'); 
		} else if(spec) {
			alert('<bean:message key="error.identContainsSpecialCharacter" />');		
		} else {
			$('method').value='save';
			$('connectionForm').submit();
		}
	}
	function testConnection() {
		// AJAX call
		var url = $F("url");
		var login = $F("user");
		var pwd = $F("pass");
		spec = unAllowedChars(login,'<bean:message key="allowedLoginCharacters"/>');
		if(!url || url.length==0 || url.indexOf('ldap:\/\/')!=0) {
			alert('<bean:message key="error.urlNotValid"/>'); 
		} else if(!login || login.length==0) {
			alert('<bean:message key="error.loginFieldEmpty"/>'); 
		} else if(!pwd || pwd.length==0) {
			alert('<bean:message key="error.passwordFieldEmpty"/>'); 
		}else if(spec) {
			alert('<bean:message key="error.identContainsSpecialCharacter" />');		
		} else {
			var connection = {id:"", name:"", url:"", user:"", pass:""};
			DWRUtil.getValues(connection);
			ValidationHelperDWR.isConnectionValid(isValid,connection);
		}
	}
	function isValid(ret) {
		if(ret) {
			alert('<bean:message key="info.validConnection"/>');
		} else {
			alert('<bean:message key="info.notValidConnection"/>');
		}
	}
	function saveConfig() {
		var id = $F("id");
		ConfigSaverDWR.saveConnectionConfig(message,id);
	}
	function message(ret) {
		var id = $F("id");
		if(ret) {
			alert('<bean:message key="info.saveDone"/>');
			this.self.location = '${ctx}/connection.do?method=edit&forward=connection&id='+id;
		} else {
			alert('<bean:message key="info.saveNotDone"/>');
		}
	}
// -->					
</script>
<div id="tabPanelWrapper">
	<html:form styleId="connectionForm" action="/connection" focus="url">
		<div id="tabPanel">
			<ul>
			  <li><html:link styleId="currentTab" action="/connection?method=edit&amp;forward=connection" paramId="id" paramProperty="connection.id" paramName="connectionForm"><bean:message key="connectionPoolPage.connectionTab" /></html:link></li>
			  <li><html:link action="/connection?method=edit&amp;forward=pool" paramId="id" paramProperty="connection.id" paramName="connectionForm"><bean:message key="connectionPoolPage.poolTab" /></html:link></li>
			</ul>
		</div>
		<div id="tabContent">
			<h1><bean:write property="connection.name" name="connectionForm"/></h1>
			<div id="subcontent">
					<input type="hidden" name="method" value="" id="method"/>
					<input type="hidden" name="forward" value="connection" id="forward"/>
					<html:hidden property="connection.id" styleId="id"/>
					<html:hidden property="connection.name" styleId="name"/>
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">
					<p><bean:message key="connectionPage.url" />: <html:text property="connection.url" maxlength="127" styleId="url" onmouseover="this.T_BGCOLOR='#D7D7ff';this.T_FONTFACE='Arial, Helvetica';this.T_FONTSIZE='12px';return escape('URL de connexion LDAP\<br\/\>\<i\>Ex: ldap:\/\/130.81.9.192:389\<\/i\>');"/></p>
					<p><bean:message key="connectionPage.login" />: <html:text property="connection.user" maxlength="31" styleId="user" onmouseover="this.T_BGCOLOR='#D7D7ff';this.T_FONTFACE='Arial, Helvetica';this.T_FONTSIZE='12px';return escape('Identifiant de connexion LDAP\<br\/\>\<i\>Ex: cn=admin,ou=users,o=services\<\/i\>');"/></p>
					<p><bean:message key="connectionPage.password" />: <html:password property="connection.pass" maxlength="31" styleId="pass"/></p>
					
</authz:authorize>
<authz:authorize ifAllGranted="ROLE_USER">
					<p><bean:message key="connectionPage.url" />: <bean:write name="connectionForm" property="connection.url" /></p>
					<p><bean:message key="connectionPage.login" />: <bean:write name="connectionForm" property="connection.user" /></p>
					<p><bean:message key="connectionPage.password" />: **********</p>
					<html:hidden property="connection.url" styleId="url"/>
					<html:hidden property="connection.user" styleId="user"/>
					<html:hidden property="connection.pass" styleId="pass"/>
</authz:authorize>
					<p>
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">
						<html:submit onclick="saveConnection();return false;"><bean:message key="connectionPage.validate" /></html:submit>&nbsp;
						<html:reset><bean:message key="connectionPage.cancel" /></html:reset>&nbsp;
</authz:authorize>
						<html:submit onclick="testConnection();return false;"><bean:message key="connectionPage.test" /></html:submit>
					</p>
			</div>
			<p>
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">								
				<input type="submit" value="<bean:message key='allPage.saveConfig' />" onclick="saveConfig();return false;"/>
</authz:authorize>
			</p>
		</div>
	</html:form>
</div>
